#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
使用协程模拟实现以下流程：
查找文件
打开文件
显示文件内容
'''
def coroutine(func):
    def start(*args, **kwargs):
        g = func(*args, **kwargs)
        next(g)
        return g
    return start

@coroutine
def find(target):
    while True:
        name = (yield)
        print("find %s"%name)
        target.send(name)
@coroutine
def open(target):
    while True:
        name = (yield)
        print("open %s"%name)
        target.send(name)
@coroutine
def cat():
    while True:
        name = (yield)
        print("cat %s"%name)

finder = find(open(cat()))
finder.send("hello")
print("-"*100)
finder.send("world")
print("-"*100)
finder.send("fuck")
print("-"*100)
finder.send("you")
print("-"*100)
finder.send("too")
print("-"*100)
finder.send("simple")